module ActiveTest::Asserts
  module Authentication

    # Assert the block redirects to the login
    # 
    #   assert_requires_login(:bob) { get :edit, :id => 1 }
    #
    def assert_requires_login(user = nil, &block)
      login_as(user) if user
      block.call
      assert_redirected_to login_url
    end

    # Assert the block accepts the login
    # 
    #   assert_accepts_login(:bob) { get :edit, :id => 1 }
    #
    # Accepts anonymous logins:
    #
    #   assert_accepts_login { get :list }
    #
    def assert_accepts_login(user = nil, &block)
      login_as(user) if user
      block.call
      assert @response.redirect_url != login_url
    end
  end
end